﻿layui.config({
    base: '/Lib/layui/extend/'
}).extend({
    formSelects: 'formSelects-v4'
}).use(['form', 'element',  'layer', 'table', 'laypage', 'laydate', 'jquery'], function () {
    var form = layui.form,
        $ = layui.jquery,
        table = layui.table,
        player = parent.layer || layui.layer,
        laydate = layui.laydate,
        element = layui.element;

    var examineListLogic = {
        //初始化
        init: function () {

            self = this;

            this.Parameters = {
                ac: new AsyncCaller(),    //异步方法管理
                type: 11,
                sDepartmentCode: "",
                title:[],
                field: 'Name',
                order: 'asc',
                projectAllList: "/Project/Info/LoadProjectAllList",

                listUrl: "/Staff/Examine/LoadExamineList",
                detailUrl: "/Staff/Examine/Detail",
                auditInfoUrl: "/Staff/Examine/AuditInfo",
                exportInfoUrl: "/Staff/Examine/EpxortExamine"

            };

            this.Parameters.ac
                .pushQueue(function () {
                    self.logicFunc.loadProjectAllList(function () {
                        self.Parameters.ac.notifyQueueAsyncFuncComplate();
                    });
                })
                .pushQueue(function () {
                    self.initPage();
                    self.Parameters.ac.notifyQueueAsyncFuncComplate();
                })
                .pushQueue(this.bindEvent)
                .exec();
        },
        //初始化列表
        initPage: function () {

            //日期
            laydate.render({
                elem: '#YearMothDate',
                format: 'yyyy-MM',
                type: 'month',
                value: new Date()
            });

            self.logicFunc.loadDays();
            //table加载
            GrdLayUI.table({
                dom: '#InfoList',
                height: "full-200",
                url: self.Parameters.listUrl,
                field: self.Parameters.field,
                order: self.Parameters.order,
                parameters: {
                    ProjectNo: $("#ProjectNo").val(),
                    KeyWord: $("#KeyWord").val(),
                    YearMothDate: $("#YearMothDate").val(),
                    iType: self.Parameters.type

                },
                title: self.Parameters.title,
                calback: function () {

                    //行点击事件
                    $(".AttendanceMachine").unbind('click').on('click', function (e) {
                        self.logicFunc.detail({
                            UserNo: $(this).attr("data-userno"),
                            WorkTime: $(this).attr("data-worktime"),
                        });
                    });
                }
            });
            form.render();
        },
        //绑定事件
        bindEvent: function () {

            element.on('tab(examine_list)', function (elem) {

                self.Parameters.type = $(this).attr('lay-id');

                if (self.Parameters.type==11) {
                    $("#funBtnAuditPass").show();
                } else {
                    $("#funBtnAuditPass").hide();
                }

                self.logicFunc.loadListInfo(self.Parameters.type);
            });

            //查询
            $("#funBtnSearch").on("click", function () {
                self.logicFunc.loadListInfo(self.Parameters.type);
            });

            //导出
            $("#funBtnExport").on("click", function () {
                self.logicFunc.exportInfo(self.Parameters.type);
            });

            //审核通过
            $("#funBtnAuditPass").on("click", function () {

                if (self.Parameters.type==11) {
                    self.logicFunc.auditPass();
                }
            });
        },
        /*
         * 逻辑方法
         */
        logicFunc: {

            //获取所有的工程信息
            loadProjectAllList: function (func) {
                GrdLayUI.ajaxPost({
                    url: self.Parameters.projectAllList,
                    data: "",
                    success: function (result) {
                        if (result.Code == 1000) {
                            $("#ProjectNo").empty();
                            $("#ProjectNo").append("<option value=''>所有工程项目</option>");
                            for (var i = 0; i < result.Data.length; i++) {
                                $("#ProjectNo").append("<option value=" + result.Data[i].ProjectNo + ">" + result.Data[i].ProjectName + "</option>");
                            }
                        }
                        if (func) {
                            func();
                        }
                    }
                });
            },
            /**
             * 获取天数信息
             * */
            loadDays: function () {
                //获取用户信息
                self.Parameters.title =
                    [{ type: 'checkbox', fixed: 'left' },
                    { field: 'ID', title: 'ID', hide: true },
                    { field: 'UserNo', title: '编号', width: 130, align: "center", sort: true, fixed: 'left' },
                    { field: 'Name', title: '姓名', width: 130, align: "center", sort: true, fixed: 'left' },
                    {
                        field: 'Sex', title: '性别', width: 80, align: "center", fixed: 'left',
                        templet: function (d) {
                            return GrdEnum.getValue.userSexValue(d.Sex.trim());
                        }
                    },
                    { field: 'YearMothDate', title: '年月', width: 90, align: "center" },
                    ];
                var dateTime = $("#YearMothDate").val();
                var dates = dateTime.split("-");
                var days = GrdAssist.getMonthDates(dates[0], dates[1]);

                for (var i = 1; i <= days; i++) {
                    self.Parameters.title.push({ field: i, title: i, width: 50 }); /*event: 'setSign',*/
                }

                self.Parameters.title.push({ field: 'AttendanceTotal', title: "出勤合计", width: 100, fixed: 'right' });
            },
            ///获取数据
            loadListInfo: function (type) {

                self.logicFunc.loadDays(type);

                var domID = "";
                if (type == 11) {
                    domID = "InfoList";
                } else {
                    domID = "InfoList1";
                } 

                //table加载
                GrdLayUI.table({
                    dom: '#' + domID,
                    height: "full-200",
                    url: self.Parameters.listUrl,
                    field: self.Parameters.field,
                    order: self.Parameters.order,
                    parameters: {
                        ProjectNo: $("#ProjectNo").val(),
                        KeyWord: $("#KeyWord").val(),
                        YearMothDate: $("#YearMothDate").val(),
                        iType: self.Parameters.type
                    },
                    title: self.Parameters.title,
                    calback: function () {

                        //行点击事件
                        $(".AttendanceMachine").unbind('click').on('click', function (e) {
                            self.logicFunc.detail({
                                UserNo: $(this).attr("data-userno"),
                                WorkTime: $(this).attr("data-worktime"),
                            });
                        });
                    }
                });
            },
            //导出信息
            exportInfo: function () {
                var json = {
                    parameters: {
                        ProjectNo: $("#ProjectNo").val(),
                        KeyWord: $("#KeyWord").val(),
                        YearMothDate: $("#YearMothDate").val(),
                        iType: self.Parameters.type
                    }
                };
                GrdLayUI.ajaxPost({
                    url: self.Parameters.exportInfoUrl,
                    data: json,
                    success: function (result) {
                        if (result.Code == 1000) {
                            window.open("http://" + location.host + result.Msg, "_self");
                        }
                    }
                });
            },

            //详情
            detail: function (data) {
                var index = layui.layer.open({
                    title: "修改信息",
                    type: 2,
                    resize: false,
                    area: ['800px', '570px'],
                    content: self.Parameters.detailUrl + '?data=' + GrdLayUI.zip(data),
                    end: function () {
                        $(window).unbind("resize");
                    }
                });
                //layui.layer.full(index);
            },

            //审核通过
            auditPass: function () {

                GrdLayUI.getCheckData({
                    domId: "InfoList",
                    backAction: function (data) {

                        var infos = [];
                        for (var i = 0; i < data.length; i++) {
                            infos.push({
                                UserNo: data[i].UserNo,
                                YearMothDate: data[i].YearMothDate,
                            });
                        }
                        var json = {
                            parameters: infos
                        };
                        
                        GrdLayUI.ajaxPost({
                            url: self.Parameters.auditInfoUrl,
                            data: json,
                            success: function (result) {
                                if (result.Code == 1000) {
                                    self.logicFunc.loadListInfo(self.Parameters.type);
                                } else {
                                    GrdLayUI.msg(result.Msg, "警告");
                                }
                            }
                        });
                    }
                });

            }
        },
        //工具
        tools: {

        }
    };
    examineListLogic.init();
});
